package com.web.daonila.service.bussiness;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.web.daonila.message.resp.TextMessage;
import com.web.daonila.util.MessageUtil;
import com.web.model.BookInfo;
import com.web.service.BookService;


@Component
public class TextMessageResp extends MessageRespHandler<TextMessage> implements MessageRespBase{

	Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private BookService bookService;
	
	public String start(Map<String, String> requestMap) {
		init(requestMap);
		logger.info("TextMessageResp.start():{requestMap={}}", new Object[]{requestMap.toString()});
		return run();
	}

	protected String validMessage(Map<String, String> requestMap) {
		// 回复文本消息
		TextMessage textMessage = new TextMessage();
		textMessage.setToUserName(fromUserName);
		textMessage.setFromUserName(toUserName);
		textMessage.setCreateTime(new Date().getTime());
		textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
		textMessage.setFuncFlag(0);
		String content = requestMap.get("Content");
		BookInfo bookInfo = new BookInfo();
		bookInfo.setBookName(content);
		List<BookInfo> books = bookService.findBooks(bookInfo);
		if(books.isEmpty()) {
			return "is empty";
		}	
		bookInfo = books.get(0);
		textMessage.setContent(bookInfo.getBookDesc());
		return MessageUtil.textMessageToXml(textMessage);
	}

}
